# -*- coding: utf-8 -*-

import mod.client.extraClientApi as clientApi

from gemstoneScripts.config.sys import *
from gemstoneScripts.config.blockList import *

ClientSystem = clientApi.GetClientSystemCls()
comp = clientApi.GetEngineCompFactory()


class Listener(ClientSystem):
    def __init__(self, namespace, system_name):
        super(ClientSystem, self).__init__(namespace, system_name)
        self.screen_node = None  # type: ScreenNode or None
        self.ListenEvent()
        pass

    def Destroy(self):
        self.UnListenEvent()
        pass

    def ListenEvent(self):
        self.ListenForEvent(clientApi.GetEngineNamespace(), clientApi.GetEngineSystemName(), 'UiInitFinished', self,
                            self.register_ui)
        self.ListenForEvent(clientApi.GetEngineNamespace(), clientApi.GetEngineSystemName(), 'ClientItemUseOnEvent', self,
                            self.add_water)
        self.ListenForEvent(MOD_NAME, MOD_LISTENER + 'GemstonePlatformUI', SEND_PLAYER_BAG_DATA_EVENT, self,
                            self.send_bag_data_to_ui)
        self.ListenForEvent(MOD_NAME, MOD_LISTENER + 'GemstonePlatformUI', SEND_GEMSTONE_PLATFORM_UI_DATA_EVENT, self,
                            self.send_ui_data)
        self.ListenForEvent(MOD_NAME, MOD_LISTENER + 'GemstonePlatformUI', OPEN_GEMSTONE_PLATFORM_UI_EVENT, self,
                            self.open_ui)
        pass

    def UnListenEvent(self):
        self.UnListenForEvent(clientApi.GetEngineNamespace(), clientApi.GetEngineSystemName(), 'UiInitFinished', self,
                              self.register_ui)
        self.UnListenForEvent(clientApi.GetEngineNamespace(), clientApi.GetEngineSystemName(), 'ClientItemUseOnEvent', self,
                            self.add_water)
        self.UnListenForEvent(MOD_NAME, MOD_LISTENER + 'GemstonePlatformUI', SEND_PLAYER_BAG_DATA_EVENT, self,
                              self.send_bag_data_to_ui)
        self.UnListenForEvent(MOD_NAME, MOD_LISTENER + 'GemstonePlatformUI', SEND_GEMSTONE_PLATFORM_UI_DATA_EVENT, self,
                              self.send_ui_data)
        self.UnListenForEvent(MOD_NAME, MOD_LISTENER + 'GemstonePlatformUI', OPEN_GEMSTONE_PLATFORM_UI_EVENT, self,
                              self.open_ui)
        pass

    def add_water(self, args):
        block_name = args['blockName']
        item = args['itemDict']['newItemName']
        if block_name == GEMSTONE_PLATFORM and item == 'minecraft:water_bucket':
            args['ret'] = True
        pass

    def register_ui(self, args):
        clientApi.RegisterUI(MOD_NAME, GEMSTONE_PLATFORM_UI, GEMSTONE_PLATFORM_UI_CLS_PATH, 'gemstone_platform.main')
        pass

    def open_ui(self, args):
        clientApi.PushScreen(MOD_NAME, GEMSTONE_PLATFORM_UI,
                             {'system': self,
                              'playerId': args['playerId'],
                              'water': args['water'],
                              'input': args['input'],
                              'output': args['output'],
                              'progress': args['progress'],
                              'pos': args['pos'],
                              'dim': args['dim']})
        self.screen_node = clientApi.GetTopScreen()
        pass

    def send_bag_data_to_ui(self, args):
        if self.screen_node:
            self.screen_node.update_bag_data(args)
        pass

    def send_ui_data(self, args):
        if self.screen_node:
            self.screen_node.update_ui_data(args)
        pass
